package com.steven.im.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.steven.im.R;
import com.steven.im.base.BaseActivity;
import com.steven.im.base.Constant;
import com.steven.im.entity.LoginEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.SPUtils;
import com.steven.im.utils.SaveParameterUtils;
import com.steven.im.utils.ScreenUtil;
import com.steven.im.utils.TitleBuilder;
import com.steven.im.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity {

    private static final String TAG = "LoginActivity";
    private boolean isHided = true;//明文和密码
    private String phone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.iv_eye)//显示隐藏
            ImageView ivHideOr;

    @Override
    public void setListen() {
        etPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                etPwd.setSelection(s.toString().length());
            }

            @Override
            public void afterTextChanged(Editable s) {
                if ((etPwd.getText().toString().length()) > 0) {
                    tvLogin.setBackgroundResource(R.drawable.bg_btn);
                } else {
                    tvLogin.setBackgroundResource(R.drawable.bg_btn_gray);
                }
            }
        });
    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        new TitleBuilder(this).setTitleText("登录")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public void initData() {
        phone = getIntent().getStringExtra("phone");
    }

    @OnClick({R.id.tv_findpwd, R.id.tv_login, R.id.iv_eye})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_findpwd://找回密码
                startActivity(new Intent(this, FindpwdActivity.class).putExtra("phone", phone));
                break;
            case R.id.tv_login://登录
                if (checkInifo()) {
                    toLogin();
                }
                break;
            case R.id.iv_eye://密码显示隐藏
                if (isHided) {
                    //显示
                    ivHideOr.setImageResource(R.drawable.login_eye);
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //隐藏
                    ivHideOr.setImageResource(R.drawable.login_eyehide);
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                isHided = !isHided;
                break;
        }
    }

    /**
     * 检查输入信息
     *
     * @return
     */
    private boolean checkInifo() {
        //判断密码
        if (TextUtils.isEmpty(etPwd.getText().toString())) {
            ToastUtil.showShort(this, "请输入密码");
            return false;
        }
        return true;
    }

    /**
     * 登录
     */
    private void toLogin() {
        String pwd = etPwd.getText().toString().trim();
        String token = SPUtils.get(getApplicationContext(), Constant.CommonInfo.TOKEN, "").toString();
        Task.getApiService().accountLogin(token, phone, pwd).enqueue(new MyCallback<LoginEntity>(LoginActivity.this) {
            @Override
            protected void onSuccess(Response<LoginEntity> response) {
                try {
                    if ("0".equals(response.body().getCode()) && ("true").equals(response.body().getResult().getSuccess())) {//登录成功
                        //保存session信息
                        SaveParameterUtils.saveJseSessionId(response.body().getResult().getSessionId());
                        finish();
                    } else if ("1".equals(response.body().getCode())) {//用户不存在，先注册
                        ToastUtil.showShort(LoginActivity.this, response.body().getMessage());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
